package day18;


import java.util.*;

//本类是用来测试Map接口的使用
public class MapDemo {
    public static void main(String[] args) {
        //创建Map对象
        Map<Integer, String> map = new HashMap<>();
        map.put(9527, "白骨精");
        map.put(9528, "黑熊精");
        map.put(9529, "鲤鱼精");
        map.put(9530, "黄毛怪");
        map.put(9531, "黑熊精");
        map.put(9527, "女儿国国王");
        System.out.println(map);
        System.out.println(map.hashCode());//获取哈希值
        System.out.println(map.equals("白骨精"));//判断是否相等
        System.out.println(map.isEmpty());//判断是否为空
        System.out.println(map.size());//获取长度
        System.out.println(map.containsKey("白骨精"));//判断是否包含键
        System.out.println(map.get(9530));//获取键对应的值
        System.out.println(map.remove(9529));//删除键对应的值并返回
        System.out.println(map.containsKey(9529));//判断是否包含键
        System.out.println(map.containsValue("鲤鱼精"));
        System.out.println(map);
        Map<Integer, String> map1 = new HashMap<>();
        map1.put(9527, "白骨精");
        map1.put(9538, "黄熊精");
        map.putAll(map1);
        System.out.println(map);
        Collection<String> values = map.values();
        System.out.println(values);

        Set<Integer> keySet = map.keySet();
        System.out.println(keySet);
        Iterator<Integer> it1 = keySet.iterator();
        while (it1.hasNext()) {
            Integer key = it1.next();
            String value = map.get(key);
            System.out.println(key + ":" + value);
        }

        Iterator<String> it = values.iterator();
        while (it.hasNext()) {
            System.out.println(it.next());
        }//遍历集合中的map
        System.out.println("===================");
        //遍历map中的键值对
        Set<Map.Entry<Integer, String>> entrySet = map.entrySet();
        Iterator<Map.Entry<Integer, String>> it2 = entrySet.iterator();//获取迭代器
        while (it2.hasNext()) {
            Integer key = it2.next().getKey();//获取键
            Map.Entry<Integer, String> entry = it2.next();//获取键值对
            System.out.println(entry.getKey() + ":" + entry.getValue());//获取键值对
        }


    }
}
